<!-- Created by: Danny Thompson -->

<%@page import="java.sql.*"%>
<%@page import="com.mysql.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rentflix Movie Search</title>
<link rel="stylesheet" href="css/style.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript" src="jquery.jcarousel.pack.js"></script>
<script type="text/javascript" src="jquery.easing.1.3.js"></script>
<script type="text/javascript" src="jquery.jcarousel.setup.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

<script src="jquery.touchslider.min.js"></script>

<script type="text/javascript">


function onload()
{
	document.inputForm.searchBox.value = getParameterByName("searchBox");
	document.inputForm.searchBox.focus();
	document.dropdownform.rating.value = getParameterByName("rating");
	document.dropdownform2.year.value = getParameterByName("year");
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function fastSearch()
{
   // window.location.href = 'http://localhost:8080/MovieProject/search?searchBox=' + document.inputForm.searchBox.value;
}


function showAll()
{
	document.filterform.filter.value = "all";
}
function showComedy()
{
	document.filterform.filter.value = "comedy";
}
function showDrama()
{
	document.filterform.filter.value = "drama";
}
function showAction()
{
	document.filterform.filter.value = "action";
}


</script>

	<center>
	<a href="/MovieProject">Home</a> |
	<a href="/MovieProject/Login">Login</a> |
	<a href="/MovieProject/register">Register New Account</a> |
	<a href="/MovieProject/search">View Movies</a> |
	<a href="/MovieProject/viewqueue">View Queue</a> |
	<a href="/MovieProject/MyAccount">My Account</a> |
	<a href="/MovieProject/reports">Admin: Reports</a> |
	<a href="/MovieProject/CheckInRentals">Admin: Check-in</a> |
	<a href="/MovieProject/newMovie">Admin: Add Movie</a>
	
	</center>
</head>
<body onload="onload()">
	<%
    try{
//Class.forName("com.mysql.jdbc.Driver");
 String searchFor = request.getParameter("searchBox");
 String showDescr = request.getParameter("desc");
 String sqlQuery = "select * from movies";
 String comedyQuery ="select * from movies where genre = 'comedy'";
 String dramaQuery ="select * from movies where genre = 'drama'";
 String actionQuery ="select * from movies where genre = 'action'";
 String ratingqs = request.getParameter("rating");
 String yearqs = request.getParameter("year");
 String filterqs = "";
 filterqs = request.getParameter("filter");
 
 if(searchFor != null)
 {
	 sqlQuery += " where title like '%" + searchFor +"%'";
 }
 if(yearqs != null)
 {
	 if(yearqs.equals("1970"))
		 sqlQuery = "select * from movies where year like '197%'";
	 else if(yearqs.equals("1980"))
		 sqlQuery = "select * from movies where year like '198%'";
	 else if(yearqs.equals("1990"))
		 sqlQuery = "select * from movies where year like '199%'";
	 else if(yearqs.equals("2000"))
		 sqlQuery = "select * from movies where year like '200%'";
	 else if(yearqs.equals("2010"))
		 sqlQuery = "select * from movies where year like '201%'";
	 else
		 sqlQuery = "select * from movies";
 }
 if(ratingqs != null)
 {
	 if(ratingqs.equals("r"))
		 sqlQuery = "select * from movies where rating = 'r'";
	 else if(ratingqs.equals("pg-13"))
		 sqlQuery = "select * from movies where rating = 'pg-13'";
	 else if(ratingqs.equals("pg"))
		 sqlQuery = "select * from movies where rating = 'pg'";
	 else if(ratingqs.equals("g"))
		 sqlQuery = "select * from movies where rating = 'g'";
	 else
		 sqlQuery = "select * from movies";
 }
 if(filterqs != null)
 {
	 if(filterqs.equals("comedy"))
	 	sqlQuery = "select * from movies where genre like '%comedy%'";
	 else if(filterqs.equals("drama"))
		 sqlQuery = "select * from movies where genre like '%drama%'";
	 else if(filterqs.equals("action"))
		 sqlQuery = "select * from movies where genre like '%action%'";
	 else if(filterqs.equals("all"))
		 sqlQuery = "select * from movies ";
 }
Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

       Statement statement = connection.createStatement() ;
       Statement comedystatement = connection.createStatement() ;
       Statement dramastatement = connection.createStatement() ;
       Statement actionstatement = connection.createStatement() ;

       ResultSet rs = statement.executeQuery(sqlQuery) ;
       ResultSet comedyrs = comedystatement.executeQuery(comedyQuery) ;
       ResultSet dramars = dramastatement.executeQuery(dramaQuery) ;
       ResultSet actionrs = actionstatement.executeQuery(actionQuery) ;
       
      
%>



<form name="filterform" method="get">
<input type="hidden" name="filter">
<input type="submit" value="All" onclick="showAll()">
<input type="submit" value="Comedy" onclick="showComedy()">
<input type="submit" value="Drama" onclick="showDrama()">
<input type="submit" value="Action" onclick="showAction()">

</form>

<form name="dropdownform" method="get">Filter by Rating
	<select name="rating" onchange="this.form.submit()">
		<option value="g">G</option>
		<option value="pg">PG</option>
		<option value="pg-13">PG-13</option>
		<option value="r">R</option>
	</select>
	</form>
	<form name="dropdownform2" method="get">Filter by Decade
	<select name="year" onchange="this.form.submit()">
		<option value="2010">2010s</option>
		<option value="2000">2000s</option>
		<option value="1990">1990s</option>
		<option value="1980">1980s</option>
		<option value="1970">1970s</option>
	</select>
</form>

<form name="inputForm" method="get">
	<input type="text" name="searchBox" >
	<input type="submit" value="Submit">
</form>


    <h1> Movies shown by filter</h1>
        <center>
       <table><tr >
        <%  int counter = 1;   
        while(rs.next()){  %>
         
          <td>  <a href="/MovieProject/movie?searchBox=<%= rs.getString(2)%>" >
				<img src="<%= rs.getString(8)%>" border="0" style="border:none;max-width:100%;"  /></a>
          </td>
         	
        <%if(counter %5 == 0){ %> </tr><tr> <% } counter ++; } %>
        </tr></table>
        </center>

 	<h1> Comedy</h1>
        <center>
       <table><tr >
        <%  int ccounter = 1;   
        while(comedyrs.next()){  %>
         
          <td>  <a href="/MovieProject/movie?searchBox=<%= comedyrs.getString(2)%>" >
				<img src="<%= comedyrs.getString(8)%>" border="0" style="border:none;max-width:100%;"  /></a>
          </td>
         	
        <%if(ccounter %5 == 0){ %> </tr><tr> <% } ccounter ++; } %>
        </tr></table>
        </center>

 	<h1> Drama</h1>
        <center>
       <table><tr >
        <%  int dcounter = 1;   
        while(dramars.next()){  %>
         
          <td>  <a href="/MovieProject/movie?searchBox=<%= dramars.getString(2)%>" >
				<img src="<%= dramars.getString(8)%>" border="0" style="border:none;max-width:100%;"  /></a>
          </td>
         	
        <%if(dcounter %5 == 0){ %> </tr><tr> <% } dcounter ++; } %>
        </tr></table>
        </center>

 	<h1> Action</h1>
        <center>
       <table><tr >
        <%  int acounter = 1;   
        while(actionrs.next()){  %>
         
          <td>  <a href="/MovieProject/movie?searchBox=<%= actionrs.getString(2)%>" >
				<img src="<%= actionrs.getString(8)%>" border="0" style="border:none;max-width:100%;"  /></a>
          </td>
         	
        <%if(acounter %5 == 0){ %> </tr><tr> <% } acounter ++; } %>
        </tr></table>
        </center>



<%
//**Should I input the codes here?**
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>
</body>
</html>